package com.sky.directdeal.manager;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.text.TextUtils;

import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.constants.ServiceConstants;
import com.sky.directdeal.listener.ServiceResponseListener;
import com.sky.directdeal.network.NetworkManager;
import com.sky.directdeal.network.ResponseListener;
import com.sky.directdeal.network.beans.HttpVerb;
import com.sky.directdeal.network.beans.ServiceRequest;
import com.sky.directdeal.utils.Utils;
import com.sky.local_ads.create_tell_us.CreateTellUsResponse;
import com.sky.local_ads.fetch_category.CategoryResponse;
import com.sky.local_ads.geocoder.FetchGeocoderResponse;
import com.sky.local_ads.localitylist.LocalityResponse;
import com.sky.local_ads.offer_cat_list.OfferCategoryResponse;
import com.sky.local_ads.offer_list.OfferResponse;
import com.sky.local_ads.register_app_details.RegisterAppDetailsResponse;
import com.sky.local_ads.store_list.StoreResponse;
import com.sky.local_ads.store_offers.StoreOfferResponse;
import com.sky.local_ads.sub_category.SubCategoryResponse;

public class ServiceManager {
	
	private static String serverHost="192.168.1.2";
	
	//private static String serverHost="meandyoumatrimony.com";
	
	//private static String serverHost="skyhomeenterprises.com";
	
	private static boolean isDummyService=false;
	
	public static void getStoreList(Context context,String catId,String subCatId,String localityId,final ServiceResponseListener<StoreResponse> listener){
	
		if(isDummyService){
			
			
		}
		
		String targetUrl=getServiceUrl(Constants.STORE_LIST_ENDPOINT)+catId+"&sub_category="+subCatId+"&locality_id="+localityId;
		
		ServiceRequest request=new ServiceRequest.Builder(targetUrl).verb(HttpVerb.GET).build();
	try{
		NetworkManager.executeJsonAsync(context,request, StoreResponse.class, new ResponseListener<StoreResponse>() {

			@Override
			public void onSuccess(StoreResponse response) {
				listener.onSuccess(response);
			}

			@Override
			public void onFailure(Throwable throwable, String errorResponse) {
				listener.onFailure(throwable, errorResponse);
			}

			@Override
			public Class<StoreResponse> getTargetType() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(String arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}catch(Exception e){
		listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
	}
	}
	
	//get categories list
	
public static void getCategoriesList(Context context,final ServiceResponseListener<CategoryResponse> listener){
		
		if(isDummyService){
			
		}
		
		String targetUrl=getServiceUrl(Constants.CATEGORY_LIST_ENDPOINT);
		
		ServiceRequest request=new ServiceRequest.Builder(targetUrl).verb(HttpVerb.GET).build();
	try{
		NetworkManager.executeJsonAsync(context,request, CategoryResponse.class, new ResponseListener<CategoryResponse>() {

			@Override
			public void onSuccess(CategoryResponse response) {
				listener.onSuccess(response);
			}

			@Override
			public void onFailure(Throwable throwable, String errorResponse) {
				listener.onFailure(throwable, errorResponse);
			}

			@Override
			public Class<CategoryResponse> getTargetType() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(String arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}catch(Exception e){
		listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
	}
	}
	
	
	//get subcatogories list
	
	public static void getSubCategoriesList(Context context,String catId,final ServiceResponseListener<SubCategoryResponse> listener){
		
		if(isDummyService){
			
		}
		
		ServiceRequest request=new ServiceRequest.Builder("http://"+serverHost+"/direct_deal/fetch_subcategory.php?cat_id="+catId).verb(HttpVerb.GET).build();
	try{
		NetworkManager.executeJsonAsync(context,request, SubCategoryResponse.class, new ResponseListener<SubCategoryResponse>() {

			@Override
			public void onSuccess(SubCategoryResponse response) {
				listener.onSuccess(response);
			}

			@Override
			public void onFailure(Throwable throwable, String errorResponse) {
				listener.onFailure(throwable, errorResponse);
			}

			@Override
			public Class<SubCategoryResponse> getTargetType() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(String arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}catch(Exception e){
		listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
	}
	}

	public static void getStoreOffers(Context context,String businessId,final ServiceResponseListener<StoreOfferResponse> listener){
		String targetURL=null;
		if(businessId!=null){
			targetURL="http://"+serverHost+"/direct_deal/store_offers.php?business_id="+businessId;
		}
		
		ServiceRequest request=new ServiceRequest.Builder(targetURL).verb(HttpVerb.GET).build();
		try{
			NetworkManager.executeJsonAsync(context,request, StoreOfferResponse.class, new ResponseListener<StoreOfferResponse>() {

				@Override
				public void onSuccess(StoreOfferResponse response) {
					listener.onSuccess(response);
				}

				@Override
				public void onFailure(Throwable throwable, String errorResponse) {
					listener.onFailure(throwable, errorResponse);
				}

				@Override
				public Class<StoreOfferResponse> getTargetType() {
					// TODO Auto-generated method stub
					return null;
				}

				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(String arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
		}catch(Exception e){
			listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
		}
		}
	
	
	//get all offer
	
	public static void getAllOffers(Context context,final ServiceResponseListener<OfferCategoryResponse> listener){
		String targetURL=null;
		targetURL="http://"+serverHost+"/direct_deal/fetch_offers.php";
		
		ServiceRequest request=new ServiceRequest.Builder(targetURL).verb(HttpVerb.GET).build();
		try{
			NetworkManager.executeJsonAsync(context,request, OfferCategoryResponse.class, new ResponseListener<OfferCategoryResponse>() {

				@Override
				public void onSuccess(OfferCategoryResponse response) {
					listener.onSuccess(response);
				}

				@Override
				public void onFailure(Throwable throwable, String errorResponse) {
					listener.onFailure(throwable, errorResponse);
				}

				@Override
				public Class<OfferCategoryResponse> getTargetType() {
					// TODO Auto-generated method stub
					return null;
				}

				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(String arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
		}catch(Exception e){
			listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
		}
		}
	
	//registering gcm to server
public static void putAppDetails(Context context,HashMap<String, String> requestParams,final ServiceResponseListener<RegisterAppDetailsResponse> listener){
		
		if(isDummyService){
			
		}
		ServiceRequest request=new ServiceRequest.Builder("http://"+serverHost+"/direct_deal/register_app_details.php").verb(HttpVerb.POST).addRequestParams(requestParams).build();
		try{
		NetworkManager.executeJsonAsync(context,request,RegisterAppDetailsResponse.class, new ResponseListener<RegisterAppDetailsResponse>() {

			@Override
			public void onSuccess(RegisterAppDetailsResponse response) {
				if(listener!=null){
					listener.onSuccess(response);	
				}
			}

			@Override
			public void onFailure(Throwable throwable, String errorResponse) {
if(listener!=null){
	listener.onFailure(throwable, errorResponse);
				}
			}

			@Override
			public Class<RegisterAppDetailsResponse> getTargetType() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(String arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}catch(Exception e){
		listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
	}
	}


//get offer by business

public static void getStoreOffersByBusiness(Activity context,String catId,final ServiceResponseListener<OfferResponse> listener){
	String targetURL=null;
	if(catId!=null){
		targetURL="http://"+serverHost+"/direct_deal/fetch_offers_by_cat.php?cat_id="+catId;
	}
	
	ServiceRequest request=new ServiceRequest.Builder(targetURL).verb(HttpVerb.GET).build();
	try{
		NetworkManager.executeJsonAsync(context,request, OfferResponse.class, new ResponseListener<OfferResponse>() {

			@Override
			public void onSuccess(OfferResponse response) {
				listener.onSuccess(response);
			}

			@Override
			public void onFailure(Throwable throwable, String errorResponse) {
				listener.onFailure(throwable, errorResponse);
			}

			@Override
			public Class<OfferResponse> getTargetType() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(String arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}catch(Exception e){
		listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
	}
	}

//get geocoder

	public static void getGecoderAddress(Context context,Location location,final ServiceResponseListener<FetchGeocoderResponse> listener){
		String targetURL=null;
		String latLng=location.getLatitude()+","+location.getLongitude();
		targetURL="https://maps.googleapis.com/maps/api/geocode/json?latlng="+latLng;
		
		ServiceRequest request=new ServiceRequest.Builder(targetURL).verb(HttpVerb.GET).build();
		try{
			NetworkManager.executeJsonAsync(context,request, FetchGeocoderResponse.class, new ResponseListener<FetchGeocoderResponse>() {

				@Override
				public void onSuccess(FetchGeocoderResponse response) {
					listener.onSuccess(response);
				}

				@Override
				public void onFailure(Throwable throwable, String errorResponse) {
					listener.onFailure(throwable, errorResponse);
				}

				@Override
				public Class<FetchGeocoderResponse> getTargetType() {
					// TODO Auto-generated method stub
					return null;
				}

				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(String arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
		}catch(Exception e){
			listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
		}
		}
	
//get location by keyword
	public static void getLocalityByKeyword(Context context,String keyword,final ServiceResponseListener<LocalityResponse> listener){
		String targetURL=null;
		targetURL="http://"+serverHost+"/direct_deal/fetch_locality_keyword.php?locality="+keyword;
		
		ServiceRequest request=new ServiceRequest.Builder(targetURL).verb(HttpVerb.GET).build();
		try{
			NetworkManager.executeJsonAsync(context,request, LocalityResponse.class, new ResponseListener<LocalityResponse>() {

				@Override
				public void onSuccess(LocalityResponse response) {
					listener.onSuccess(response);
				}

				@Override
				public void onFailure(Throwable throwable, String errorResponse) {
					listener.onFailure(throwable, errorResponse);
				}

				@Override
				public Class<LocalityResponse> getTargetType() {
					// TODO Auto-generated method stub
					return null;
				}

				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(String arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
		}catch(Exception e){
			listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
		}
		}
	
	//get locality list
	public static void getLocalityList(Context context,final ServiceResponseListener<LocalityResponse> listener){
		String targetURL=null;
		targetURL=getServiceUrl(Constants.ALL_LOCATION_LIST_ENDPOINT);//"http://"+serverHost+"/direct_deal/fetch_locality.php";
		
		ServiceRequest request=new ServiceRequest.Builder(targetURL).verb(HttpVerb.GET).build();
		try{
			NetworkManager.executeJsonAsync(context,request, LocalityResponse.class, new ResponseListener<LocalityResponse>() {

				@Override
				public void onSuccess(LocalityResponse response) {
					listener.onSuccess(response);
				}

				@Override
				public void onFailure(Throwable throwable, String errorResponse) {
					listener.onFailure(throwable, errorResponse);
				}

				@Override
				public Class<LocalityResponse> getTargetType() {
					// TODO Auto-generated method stub
					return null;
				}

				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(String arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
		}catch(Exception e){
			listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
		}
		}
	
	public static String getServiceUrl(String key) {

		if (TextUtils.isEmpty(key)) {
			return Constants.EMPTY;
		}
		
		return Utils.getResponse().getdata().getApiservice().get(Constants.API_SERVICE_URL_KEY) + Utils.getResponse().getdata().getApiservice().get(key);

	}
	
	//saving tell us query
public static void putTellUsQuery(Context context,HashMap<String, String> requestParams,final ServiceResponseListener<CreateTellUsResponse> listener){
		
		if(isDummyService){
			
		}
		
		String tartgetUrl=getServiceUrl(Constants.TELL_US_QUERY_ENDPOINT);
		ServiceRequest request=new ServiceRequest.Builder(tartgetUrl).verb(HttpVerb.POST).addRequestParams(requestParams).build();
		try{
		NetworkManager.executeJsonAsync(context,request,CreateTellUsResponse.class, new ResponseListener<CreateTellUsResponse>() {

			@Override
			public void onSuccess(CreateTellUsResponse response) {
				if(listener!=null){
					listener.onSuccess(response);	
				}
			}

			@Override
			public void onFailure(Throwable throwable, String errorResponse) {
if(listener!=null){
	listener.onFailure(throwable, errorResponse);
				}
			}

			@Override
			public Class<CreateTellUsResponse> getTargetType() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(String arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}catch(Exception e){
		listener.onFailure(e.getCause(),ServiceConstants.NETWORk_ERROR );
	}
	}


}
